//
//  M4UploadAlertView.swift
//  M4
//
//  Created by YuChuang on 2024/8/20.
//

import UIKit

class M4UploadAlertView: UIView {
    
    @IBOutlet weak var M4UploadBGView: UIView!
    
    @IBOutlet weak var M4UploadProgressLabel: UILabel!
    
    @IBOutlet weak var M4UploadProgressView: UIView!
    
    @IBOutlet weak var M4UploadProgressWidth: NSLayoutConstraint!
    
    @IBOutlet weak var M4UploadDescribe: UILabel!
    
    @IBOutlet weak var M4ContactButton: UIButton!
    
    @IBOutlet weak var M4ContactButtonHeight: NSLayoutConstraint!
    
    @IBOutlet weak var M4ContactButtonTop: NSLayoutConstraint!
    
    private var M4UploadSliderViewWidth: CGFloat = 0
    
    var M4AnimationRun: Bool = true
    
    private var M4Progress: CGFloat = 0

    override func awakeFromNib() {
        self.frame = CGRect(x: 0, y: 0, width: M4ScreenWidth, height: M4ScreenHeight)
        M4UploadDescribe.text = "La cuota de evaluación de IA está en progreso, espere."
        M4ContactButton.isHidden = true
        M4ContactButtonHeight.constant = 0
        M4ContactButtonTop.constant = 0
    }
    
    
    func M4UploadStartAnimation() {
//        DT_AMXProgress_bgView.layoutIfNeeded()
        let width = M4ScreenWidth - 60 * 2
        if M4AnimationRun == false { return }
        let a  = width * 0.99
        let b = a / 100
        M4Progress += 0.1
        if M4Progress >= 9.9 {
            self.M4UploadProgressLabel.text = "99%"
            return
        }
        self.M4UploadSliderViewWidth = self.M4UploadSliderViewWidth + b
        M4UploadProgressView?.layoutIfNeeded()
        UIView.animate(withDuration: 0.07) {
            self.M4UploadProgressWidth.constant = self.M4UploadSliderViewWidth
            self.M4UploadProgressView.superview!.layoutIfNeeded()
        } completion: { r in
            self.M4UploadStartAnimation()
        }
        M4UploadProgressLabel.text = "\(Int(M4Progress*10))%"
    }
    
    func M4UploadStopAnimation() {
//        DT_AMXProgress_bgView.layoutIfNeeded()
        self.M4UploadSliderViewWidth = M4ScreenWidth - 60 * 2
        self.M4UploadProgressLabel.text = "100%"
        UIView.animate(withDuration: 1) {
            self.M4UploadProgressWidth.constant = self.M4UploadSliderViewWidth
            self.M4UploadProgressView.superview!.layoutIfNeeded()
        } completion: { r in
            self.M4AlertViewHidden()
        }
    }
    
    func M4UploadChangeUI() {
        self.M4UploadSliderViewWidth = (M4ScreenWidth - 60 * 2) - 7
        self.M4UploadProgressLabel.text = "99%"
        UIView.animate(withDuration: 1) {
            self.M4UploadProgressWidth.constant = self.M4UploadSliderViewWidth
            self.M4UploadProgressView.superview!.layoutIfNeeded()
        } completion: { r in
            self.M4UploadDescribe.text = "Espere pacientemente, comuníquese con el servicio de atención al cliente si excede los 5 minutos."
            self.M4ContactButton.isHidden = false
            self.M4ContactButtonHeight.constant = 38
            self.M4ContactButtonTop.constant = 20
        }
    }
    
    
    @IBAction func M4ContactButtonClick(_ sender: UIButton) {
        self.M4AlertViewHidden()
        let alert = M4KFAlertView()
        alert.didTapKFzxHandle = {
            
            
        }
        alert.M4AlertViewShow()
        sender.M4EventUnavailable = false
    }
    

}
